/******************************************************************************
 * Copyright (C) 2012 ShenZhen ComTop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.code.common.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

/**
 * MAP和VO装换辅助类
 * 
 * @author 陈大周
 * @since 1.0
 * @version 2012-12-8 陈大周
 */
public class MaptoVOHelper {
    
    /** Log对象 */
    private final static Logger LOG = Logger.getLogger(MaptoVOHelper.class);
    
    public MaptoVOHelper() {
    }
    
    /**
     * List<Map> to List<VO>转换
     * 
     * @param lstMap
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List<?> mapToVOForList(List<Map<String, Object>> lstMap, Class clazz) {
        Object object = null;
        List<Object> lstVO = new ArrayList<Object>();
        if (lstMap == null) {
            return lstVO;
        }
        for (int i = 0; i < lstMap.size(); i++) {
            object = org.springframework.beans.BeanUtils.instantiate(clazz);
            try {
                BeanUtils.populate(object, lstMap.get(i));
            } catch (IllegalAccessException e) {
                LOG.error("Map转VO异常", e);
            } catch (InvocationTargetException e) {
                LOG.error("Map转VO异常", e);
            }
            lstVO.add(object);
        }
        return lstVO;
    }
    
    /**
     * Map转换成VO
     * 
     * @param objMap
     * @param clazz
     * @return VO
     */
    @SuppressWarnings("unchecked")
    public static <T> T mapToVO(Map objMap, Class<T>  beanClass) {
        T bean=null;
        try {
            bean = beanClass.newInstance();
            BeanUtils.populate(bean, objMap);
        } catch (IllegalAccessException e) {
            LOG.error("Map转VO异常", e);
        } catch (InvocationTargetException e) {
            LOG.error("Map转VO异常", e);
        } catch (InstantiationException e) {
            LOG.error("Map转VO异常", e);
        }
        return bean;
    }
    
    /**
     * 转换ABC_ABC到abcAbc
     * 
     * @param str
     * @return string
     */
    private static String toCharLower(String str) {
        
        char[] ch = str.toCharArray();
        StringBuffer buffer = new StringBuffer();
        buffer.append(Character.toUpperCase(ch[0]));
        if (str.length() > 1) {
            for (int i = 1; i < str.length(); i++) {
                if (ch[i] >= 'A' && ch[i] <= 'Z') {
                    buffer.append(Character.toLowerCase(ch[i]));
                }
            }
        }
        return buffer.toString();
    }
    
    /**
     * 转换ABC_ABC到abcAbc
     * 
     * @param str
     * @return string
     */
    public static String transformChar(String str) {
        StringBuffer sbStr = new StringBuffer(100);
        String[] strs = str.split("_");
        if (strs != null) {
            
            for (int i = 0; i < strs.length; i++) {
                if (i == 0) {
                    sbStr.append(strs[i].toLowerCase());
                } else {
                    sbStr.append(toCharLower(strs[i]));
                }
                
            }
        }
        return sbStr.toString();
    }
}